<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Category;
use App\Repositories\CategoryRepository;

class CategoryController extends Controller
{
    /**
     * The category repository instance.
     *
     * @var CategoryRepository
     */
    protected $categorys;

    /**
     * Create a new controller instance.
     *
     * @param  CategoryRepository  $categorys
     * @return void
     */
    public function __construct( CategoryRepository $categorys)
    {
        $this->middleware('auth');

        $this->categorys = $categorys;
    }

    /**
     * Display a list of all of the user's task.
     *
     * @param  Request  $request
     */
    public function index(Request $request)
    {
    	$categorys = $this->categorys->forUser($request->user(), $needPage=true);
    	
        return view('categorys.index', [
            'categorys' => $categorys,
        ]);
    }
    
    /**
     * Create a new note.
     *
     * @param  Request  $request
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
        ]);
        
        $arr = array('name'=>$request,'category_order'=>$this->categorys->forLastCategoryOrder($request->user()));
        
        $note = $request->user()->categorys()->create($request->all());

        if ($request->ajax() || $request->wantsJson()) {
        	$resp = $this->responseJson(self::OK_CODE);
        	return response($resp);
        } else {
        	return redirect('/categorys')->with('message', 'IT WORKS!');
        }
        
    }

    /**
     * Destroy the given task.
     *
     * @param  Request  $request
     * @param  Category  $category
     */
    public function destroy(Request $request, Category $category)
    {
        $this->authorize('destroy', $category);
        
        if(empty($category->feeds) || count($category->feeds) == 0){
        	$category->delete();
        } else {
        	$resp = $this->responseJson(1000,null,'This category has Feeds!cannot delete!');
        	return response($resp);
        }

        if ($request->ajax() || $request->wantsJson()) {
        	$resp = $this->responseJson(self::OK_CODE);
        	return response($resp);
        } else {
        	return redirect('/categorys')->with('message', 'IT WORKS!');
        }
    }
    
    public function update(Request $request, Category $category)
    {
    	$this->authorize('destroy', $category);
    	
    	if($request->method() == 'GET'){
    		return view('categorys.update',array('category'=>$category));
    	}
    	
    	$this->validate($request, [
    			'name' => 'required',
    	]);
    
    	$category->update($request->all());
    
    	if ($request->ajax() || $request->wantsJson()) {
    		$resp = $this->responseJson(self::OK_CODE);
    		return response($resp);
    	} else {
    		return redirect('/categorys')->with('message', 'IT WORKS!');
    	}
    }
    
    public function sort(Request $request)
    {
    	$this->validate($request, [
    		'category_ids' => 'required',
    	]);
    	
    	$category_ids_arr = explode(',', $request->category_ids);

    	$sort = 0;
    	foreach ($category_ids_arr as $category_id){
    		$category = $this->categorys->forCategoryId($request->user(), $category_id);
    		if(!empty($category)){
		    	$category->update(array('category_order' => $sort++));
    		}
    	}
    	
    	if ($request->ajax() || $request->wantsJson()) {
    		$resp = $this->responseJson(self::OK_CODE);
    		return response($resp);
    	} else {
    		return redirect('/categorys')->with('message', 'IT WORKS!');
    	}
    }
}
